<?php

namespace Home\Controller;

use Home\Controller;

/**
 * 用户管理
 */
class MemberController extends BaseController
{
    /**
     * 用户列表
     * @return [type] [description]
     */
    public function index($key = "")
    {
        if ($key == "") {
            $where['id'] = session('homeId');
            $model = M('member');
        } else {
            $where['username'] = trim($key);
            $model = M('member');
        }

        $count = $model->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Extend\Page($count, 20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $member = $model->limit($Page->firstRow . ',' . $Page->listRows)->where($where)->order('id asc')->select();
        $this->assign('list', $member);
        $this->assign('page', $show);
        $this->se_id = session('homeId');
        $result = $model->where(array('id' => session('homeId')))->find();
        $this->result = $result;

        $uid = session('homeId');

        $model = M('member')->find($uid);
        $this->fid = $uid;
        $this->model = $model;

        $this->display("update");
    }

    public function jiekou()
    {
        //默认显示添加表单
        if (!IS_POST) {
            $id = session('homeId');
            $this->fid = $id;
            $this->listcount = M("list")->where(array('fid' => $id))->count();
            $model = M('member')->find($id);
            $this->assign('result', $model);
            $this->display();
        }

    }

//    public function combo()
//    {
//
//        if (is_mobile()) {
//            $this->assign('ispc', '0');
//        } else {
//            $this->assign('ispc', '1');
//        }
//
//        if (!IS_POST) {
//            $id = session('homeId');
//            $this->fid = $id;
//            $model = M('member')->find($id);
//            $this->assign('result', $model);
//            $this->display();
//        }
//    }
    public function combo()
    {

        if (is_mobile()) {
            $this->assign('ispc', '0');
        } else {
            $this->assign('ispc', '1');
        }

        if (!IS_POST) {
            $id = session('homeId');
            $this->fid = $id;
            $model = M('member')->find($id);
            $dow = M('Download')->where("isshow = 1")->order('price')->select();
            foreach ($dow as $key=>$value){
                if($key == 0){
                    $dow[$key]['class'] = "class='coverage yellow'";
                }else if($key == 1){
                    $dow[$key]['class'] =  "class='green' style='left: 286px;'";
                } else if($key == 2){
                    $dow[$key]['class'] =  "class='coverage red' style='left:572px;'";
                }else if($key == 3){
                    $dow[$key]['class'] =  "class='blue' style='left: 858px;'";
                }
            }

            $this->assign('result', $model);
            $this->assign('data', $dow);
            $this->display();
        }
    }

    public function balance()
    {
        //默认显示添加表单
        if (!IS_POST) {
            $id = session('homeId');
            $this->fid = $id;
            $this->listcount = M("list")->where(array('fid' => $id))->count();
            $model = M('member')->find($id);
//            //$order = M('Orders')->where("userid = $id")->select();
//
            $money = M()->query("select a.listid as listid ,b.fid as fid, b.nickname,a.userid, a.addtime,a.money as money  ,b.id as id from lc_orders a , lc_list b WHERE a.listid = b.id and b.fid = $id  ORDER BY a.addtime desc");
            $order = M()->query("SELECT a.userid,a.listid,a.money,a.addtime,a.note,a.paytype,b.nickname FROM lc_orders a LEFT JOIN  lc_list b on a.listid = b.id WHERE userid =  $id  ORDER BY a.addtime desc");

            $this->money=$money;
            $this->order=$order;
            $this->assign('result', $model);
            $this->display();
        }

    }

    public function ccset()
    {
        //默认显示添加表单
        if (!IS_POST) {
            $id = session('homeId');
            $this->fid = $id;
            $this->listcount = M("list")->where(array('fid' => $id))->count();
            $model = M('member')->find($id);
            $this->assign('result', $model);
            $this->display();
        }

    }

    /**
     * 添加用户
     */
    public function add()
    {
        //默认显示添加表单
        if (!IS_POST) {
            $this->fid = session('homeId');
            $this->display();
        }
        if (IS_POST) {
            //如果用户提交数据
            $config = M('Config');
            $con_info = $config->where('id=1')->find();
            if ($con_info['user'] === '0') {
                if (session('homename') !== 'admin') {
                    $this->error("当前暂停提交,处理问题,谢谢合作!");
                }
            }

            $model = D("Member");
            if (!$model->create()) {
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($model->getError());
                exit();
            } else {
                if ($model->add()) {
                    $this->success("用户添加成功", U('index/index'));
                } else {
                    $this->error("用户添加失败");
                }
            }
        }
    }

    /**
     * 更新管理员信息
     * @param  [type] $id [管理员ID]
     * @return [type]     [description]
     */
    public function update()
    {
        //默认显示添加表单
        if (!IS_POST) {
            $id = I('id');
            $uid = session('homeId');
            if ($id !== $uid) {
                $this->error('请正确提交');
            }

            $model = M('member')->find($id);
            $this->fid = $uid;
            $this->assign('model', $model);
            $this->display();
        }
        if (IS_POST) {
            $model = D("Member");
            if (!$model->create()) {
                $this->error($model->getError());
            } else {
                //验证密码是否为空   
                $data = I();
                unset($data['password']);
                if (I('password') != "") {
                    $data['password'] = md5(I('password'));
                }
                //强制更改超级管理员用户类型
                if (C('SUPER_ADMIN_ID') == I('id')) {
                    $data['type'] = 2;
                }
                $uid = session('homeId');
                if ($_POST['id'] !== $uid) {
                    $this->error('请正确提交');
                }
                //更新
                if ($model->save($data)) {
                    session('homeId', null);
                    session('homename', null);
                    $this->success("用户信息更新成功,请从新登录!", U('member/index'));
                } else {
                    $this->error("未做任何修改,用户信息更新失败");
                }
            }
        }
    }

    public function amendEmail()
    {
        //默认显示添加表单
        if (!IS_POST) {
            //   $id = I('id');
            $id = session('homeId');
//            if ($id !== $uid) {
//                $this->error('请正确提交');
//            }

            $model = M('member')->find($id);
            $member = M('Member');
            $result = $member->where(array('id' => session('homeId')))->find();
            $this->result = $result;
            $this->fid = $id;
            $this->assign('model', $model);
            $this->display();
        }
        if (IS_POST) {

            $checkcode = I('checkcode');
            $config = M("Config")->find();
            $_POST['allowsize'] = $config['allowsize'];
            $_POST['singlesize'] = $config['singlesize'];
            $_POST['uptype'] = $config['uptype'];
            $_POST['tianshu'] = $config['tianshu'];
            $_POST['everydaynum'] = $config['freedownnum1'];
            $_POST['freedate'] = date('Ymd', time());

            $model = D("Member");
            $username = I('username');
            if (strlen($username) > 25) {
                $this->error('此邮箱太长，请从新换一个邮箱！');
            }
            if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
                $this->error('邮箱不正确！');
            }
            if (empty($username)) {
                $this->error('邮箱不能为空');
            }
            if (!$model->create()) {
                $this->error($model->getError());
            } else {

                //验证密码是否为空
                $data = I();
                $data['username'] = $username;
                $uid = session('homeId');
                $userinfo = M('member')->find($uid);
                $user = $model->where(array('username' => $username))->find();

                if ($user) {
                    $this->error('此邮箱已存在！');
                }

                unset($data['password']);


                if (md5(I('password')) != $userinfo['password']) {
                    $this->error('请输入正确的密码');
                }


                if ($_POST['id'] !== $uid) {
                    $this->error('请正确提交');
                }

                //更新
                if ($model->save($data)) {
                    session('homeId', null);
                    session('homename', null);
                    $this->success("用户信息更新成功,请从新登录!", U('member/index'));
                } else {
                    $this->error("未做任何修改,用户信息更新失败");
                }
            }
        }
    }

    /**
     * 删除管理员
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete($id)
    {
        if (C('SUPER_ADMIN_ID') == $id) $this->error("超级管理员不可禁用!");
        $model = M('member');
        //查询status字段值
        $result = $model->find($id);
        //更新字段
        $data['id'] = $id;
        if ($result['status'] == 1) {
            $data['status'] = 0;
        }
        if ($result['status'] == 0) {
            $data['status'] = 1;
        }
        if ($model->save($data)) {
            $this->success("状态更新成功", U('member/index'));
        } else {
            $this->error("状态更新失败");
        }
    }

    /**
     * 保存七牛个人配置信息
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function config()
    {
        if (IS_POST) {

            $uid = session('homeId');
            if ($_POST['id'] !== $uid) {
                $this->error('请正确提交');
            }
            $_POST['domain'] = str_replace(' ', '', trim($_POST['domain']));
            if (substr($_POST['domain'], 0, 7) !== 'http://') {
                $_POST['domain'] = 'http://' . $_POST['domain'];
            }
            if (substr($_POST['domain'], -1) !== '/') {
                $_POST['domain'] = $_POST['domain'] . '/';
            }
            $model = D("Member");
            if (!$model->create()) {
                $this->error($model->getError());
            } else {
                //更新
                if ($model->save()) {
                    $this->success("更新成功!", U('member/jiekou'));
                } else {
                    $this->error("更新失败");
                }
            }
        }
    }

    public function config22()
    {
        if (IS_POST) {

            $uid = session('homeId');
            if ($_POST['id'] !== $uid) {
                $this->error('请正确提交');
            }
            $_POST['isggconfig'] = trim($_POST['isggconfig']);

            $model = D("Member");
            if (!$model->create()) {
                $this->error($model->getError());
            } else {
                //更新
                if ($model->save()) {
                    $this->success("更新成功!", U('member/ccset'));
                } else {
                    $this->error("更新失败");
                }
            }
        }
    }

    public function changepwd()
    {
        //默认显示添加表单
        if (!IS_POST) {
            $uid = session('homeId');

            $model = M('member')->find($uid);
            $member = M('Member');
            $result = $member->where(array('id' => session('homeId')))->find();
            $this->result = $result;
            $this->fid = $uid;
            $this->assign('model', $model);
            $this->display();
        }
        if (IS_POST) {
            $model = D("Member");
            if (!$model->create()) {
                $this->error($model->getError());
            } else {
                $data = I();
                //验证密码是否为空   
                $uid = session('homeId');

                $userinfo = M('member')->find($uid);
                if (md5($data['oldpassword']) != $userinfo['password']) {
                    $this->error('旧密码不正确');
                }
                if ($data['newpassword'] == "") {
                    $this->error('新密码不能为空');
                }
                if ($data['newpassword'] != $data['newrepassword']) {
                    $this->error('两次密码不一致');
                }
                if ($_POST['id'] !== $uid) {
                    $this->error('请正确提交');
                }
                $data['password'] = md5($data['newpassword']);
                unset($data['newpassword']);
                unset($data['oldpassword']);
                unset($data['newrepassword']);
                //更新
                if ($model->save($data)) {
                    $this->success("密码修改成功!", U('member/index'));
                } else {
                    $this->error("未做任何修改,用户信息更新失败");
                }
            }
        }
    }


    public function ticket()
    {
        $uid = session('homeId');
        $model = M('member');
        $model = $model->find($uid);
        $this->fid = $uid;
        $this->assign('model', $model);
        $result = M('member')->where(array('id' => session('homeId')))->find();
        $this->result = $result;
        $where = array();
        $where['userid'] = session('homeId');
        $model = M('Ticket');


        $num_my = 20;
        $count = $model->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Extend\Page($count, $num_my);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $list = $model->limit($Page->firstRow . ',' . $Page->listRows)->where($where)->order('id DESC')->select();

        $dqy = I('get.p') ? I('get.p') : 1;

        $this->assign('list', $list);
        $this->assign('page', $show);


        $this->display();

    }

    public function newticket()
    {
        if (!IS_POST) {
            $uid = session('homeId');

            $model = M('member')->find($uid);
            $this->fid = $uid;
            $this->assign('model', $model);


            $this->display();
        }
        if (IS_POST) {


            if (!trim(I('post.typename'))) {
                $this->error('分类不能为空');
                exit;
            }
            if (!trim(I('post.desc'))) {
                $this->error('问题描述不能为空');
                exit;
            }
            if (!trim(I('post.tel'))) {
                $this->error('手机号不能为空');
                exit;
            }
            if (!trim(I('post.email'))) {
                $this->error('邮箱不能为空');
                exit;
            }
            if (!trim(I('post.tname'))) {
                $this->error('姓名不能为空');
                exit;
            }
            $nowtime = time();
            $uid = session('homeId');
            $tno = 'KF' . date('YmdHis', $nowtime) . rand(1000, 9999);
            $data = array();
            $data['typename'] = trim(I('post.typename'));
            $data['desc'] = trim(I('post.desc'));
            $data['jimidesc'] = trim(I('post.jimidesc'));
            $data['tel'] = trim(I('post.tel'));
            $data['tname'] = trim(I('post.tname'));
            $data['email'] = trim(I('post.email'));
            $data['addtime'] = date('Y-m-d H:i:s', $nowtime);
            $data['status'] = '0';
            $data['userid'] = $uid;
            $data['tno'] = $tno;
            $tid = M("Ticket")->add($data);

            $data = array();
            $data['desc'] = trim(I('post.desc'));
            $data['addtime'] = date('Y-m-d H:i:s', $nowtime);
            $data['userid'] = $uid;
            $data['tid'] = $tid;
            $trid = M("Ticketreply")->add($data);

            $this->success("提交成功", U('member/ticket'));

        }
    }

    public function view_ticket()
    {
        //默认显示添加表单
        if (!IS_POST) {
            $id = I('id');
            $uid = session('homeId');

            $model = M('member')->find($uid);
            $this->fid = $uid;
            $this->assign('model', $model);

            $info = M('Ticket')->find($id);
            $this->assign('info', $info);

            $list = M('Ticketreply')->where(array('tid' => $id))->order('id asc')->select();

            $this->assign('list', $list);
            $this->display();
        }
        if (IS_POST) {
            $id = I('post.id');
            $tid = $id;
            if (!trim(I('post.desc'))) {
                $this->error('问题描述不能为空');
                exit;
            }
            $nowtime = time();
            $uid = session('homeId');

            $data = array();
            if (trim(I('post.desc'))) {
                $data['jimidesc'] = trim(I('post.jimidesc'));
            }
            $data['status'] = "0";
            $tid = M("Ticket")->where(array("id" => $id))->save($data);

            $data = array();
            $data['desc'] = trim(I('post.desc'));
            $data['addtime'] = date('Y-m-d H:i:s', $nowtime);
            $data['userid'] = $uid;
            $data['tid'] = $id;
            $trid = M("Ticketreply")->add($data);

            $this->success("提交成功", U('member/view_ticket', array("id" => $id)));
        }
    }

    public function close_ticket()
    {
        $id = I('id');
        $model = M('Ticket');

        $data['id'] = $id;
        $data['status'] = 2;

        if ($model->save($data)) {
            $this->success("关闭成功", U('member/ticket'));
        } else {
            $this->error("关闭失败");
        }
    }

    public function delete_ticket()
    {
        $id = I('id');
        $model = M('Ticket');

        $data['id'] = $id;
        $model->where($data)->delete();
        M("Ticketreply")->where(array("tid" => $id))->delete();
        $this->success("删除成功", U('member/ticket'));

    }

    //QQ54885782 充值
   /* public function buydown()
    {

        if (is_mobile()) {
            $this->assign('ispc', '0');
        } else {
            $this->assign('ispc', '1');
        }

        if (!IS_POST) {
            $uid = session('homeId');

            $model = M('member')->find($uid);
            $this->fid = $uid;
            $this->assign('model', $model);

            $this->display();
        }

        if (IS_POST) {


            $taocanarray = array(
                '1' => '10',
                '35' => '1000',
                '100' => '3000',
                '160' => '5000',
                '300' => '10000',
            );
            $plusnum = 0;
            $paytype = I('post.paytype');
            $buymoney = I('post.buymoney');
            $buymoney = (int)$buymoney;
            if (!isset($taocanarray[$buymoney])) {
                $this->error('参数错误');
                exit;
            } else {
                $plusnum = $taocanarray[$buymoney];
            }
            if ($paytype != 1) {
                $this->error('参数错误');
                exit;
            }
            $nowtime = time();
            $uid = session('homeId');
            $orderid = date('YmdHis', time()) . rand(100, 999) . $userid;

            $userinfo = M('member')->find($uid);
            if ($userinfo['mymoney'] < $buymoney) {
                $this->error('余额不足', U('member/index'));
                exit;
            }
            $userarr = M("Member")->where(array('id' => $info['fid']))->find();

            $res22 = M("Member")->where(array('id' => $uid))->setDec('mymoney', $buymoney);
            $res33 = M("Member")->where(array('id' => $uid))->setInc('downnum', $plusnum);

            $data = array();
            $data['orderid'] = $orderid;
            $data['userid'] = $uid;
            $data['addtime'] = date('Y-m-d H:i:s', time());
            $data['buymoney'] = $buymoney;
            $data['status'] = '1';
            $data['paytype'] = '0';
            $trid = M("taocanorder")->add($data);

            $this->success("购买成功", U('member/index'));
        }


    } */
    public function buydown()
    {

        if (is_mobile()) {
            $this->assign('ispc', '0');
        } else {
            $this->assign('ispc', '1');
        }

        if (!IS_POST) {
            $uid = session('homeId');

            $model = M('member')->find($uid);
            $this->fid = $uid;
            $this->assign('model', $model);

            $this->display();
        }

        if (IS_POST) {


//            $taocanarray = array(
//                '1' => '10',
//                '35' => '1000',
//                '100' => '3000',
//                '160' => '5000',
//                '300' => '10000',
//            );
            $taocanarray = array();
           $download= M('Download')->where()->select();
           foreach ($download as $key=>$value){
               $taocanarray[$value['price']]=$value['downum']+$value['giftsnum'];
           }

            $plusnum = 0;
            $paytype = I('post.paytype');
            $buymoney = I('post.buymoney');
            $buymoney = $buymoney;
            if (!isset($taocanarray[$buymoney])) {
                $this->error('参数错误',U('Member/combo'));
                exit;
            } else {
                $plusnum = $taocanarray[$buymoney];
            }
            if ($paytype != 1) {
                $this->error('参数错误');
                exit;
            }
            $nowtime = time();
            $uid = session('homeId');
            $orderid = date('YmdHis', time()) . rand(100, 999) . $uid;

            $userinfo = M('member')->find($uid);
            if ($userinfo['mymoney'] < $buymoney) {
                $this->error('余额不足', U('member/index'));
                exit;
            }
           // $userarr = M("Member")->where(array('id' => $info['fid']))->find();

            $res22 = M("Member")->where(array('id' => $uid))->setDec('mymoney', $buymoney);
            $res33 = M("Member")->where(array('id' => $uid))->setInc('downnum', $plusnum);

            $data = array();
            $data['orderid'] = $orderid;
            $data['userid'] = $uid;
            $data['addtime'] = date('Y-m-d H:i:s', time());
            $data['buymoney'] = $buymoney;
            $data['status'] = '1';
            $data['paytype'] = '0';
            $trid = M("taocanorder")->add($data);
            if($trid) {

                $list['orderno'] = $orderid;
                $list['userid'] = $uid;
                $list['listid'] = 0;
                $list['money'] = $buymoney;
                $list['addtime'] = date('Y-m-d H:i:s', time());
                $list['status'] = '1';
                $list['note'] = '购买套餐';
                $list['paytype'] = 0;
                $orid =   M('Orders')->add($list);
                $this->success("购买成功", U('member/combo'));
            }


        }


    }

    /**
     * 账户余额
     */
//    public function balance(){
//
//    }

}
